//
//  RHPolicyConfigView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/13.
//

#import "RHPolicyConfigView.h"
#import "RHSocCircleView.h"

@interface RHPolicyConfigView()
@property (nonatomic ,strong) UIView *contentView;

@property (nonatomic ,strong) MASConstraint *bgViewTopConstraint;

@property (nonatomic ,strong) MASConstraint *bgViewBottomConstraint;

@property (nonatomic ,strong) RHSocCircleView *socCircleView;

@property (nonatomic ,strong) QMUITextField *textField;
@end
@implementation RHPolicyConfigView

-(instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

-(void)setCurMode:(RHPolicyModeModel *)curMode
{
    _curMode = curMode;
    self.titleLab.text = curMode.time;
    self.socCircleView.progress = [curMode.soc integerValue]/100.0;
    self.textField.text = curMode.power;
    
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self addSubview:touchView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *contentView = kView;
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.cornerRadius = 8;
    contentView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);
       
    }];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setText:@"模式选择".rh_localized];
    [_titleLab setTextAlignment:NSTextAlignmentCenter];
    [_titleLab setFont:kBOLDSYSTEMFONT(18)];
    [_titleLab setTextColor:FontColor(@"#3F4443")];
    _titleLab.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:_titleLab];
    
    //充放电SOC
    QMUILabel *chgLab = [[QMUILabel alloc] init];
    [chgLab setText:[NSString stringWithFormat:@"%@SOC (%%)",@"充放电".rh_localized]];
    [chgLab setTextAlignment:NSTextAlignmentCenter];
    [chgLab setFont:kSYSTEMFONT(16)];
    [chgLab setTextColor:kCOLOR_HEX_A(@"3F4443", 0.8)];
    [contentView addSubview:chgLab];
    [chgLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(15);
    }];
    
    QMUILabel *chgTipsLab = [[QMUILabel alloc] init];
    [chgTipsLab setText:@"设置SOC阀值，当电池容量降至设定SOC阀值，系统将不在进行充放电。".rh_localized];
    [chgTipsLab setTextAlignment:NSTextAlignmentCenter];
    [chgTipsLab setFont:kSYSTEMFONT(12)];
    [chgTipsLab setTextColor:kCOLOR_HEX_A(@"3F4443", 0.6)];
    [contentView addSubview:chgTipsLab];
    [chgTipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(chgLab.mas_bottom).offset(15);
    }];
    
    
    UIView *circleContentView = [[UIView alloc]init];
    circleContentView.backgroundColor = CFTabViewBgColor;
    circleContentView.layer.cornerRadius = 8;
    [contentView addSubview:circleContentView];
    [circleContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(kScaleW(130));
        make.top.equalTo(chgTipsLab.mas_bottom).offset(15);
       
    }];
    
    _socCircleView = [[RHSocCircleView alloc]init];
    _socCircleView.backgroundColor = CFTabViewBgColor;
    _socCircleView.layer.cornerRadius = 8;
    _socCircleView.layer.maskedCorners = YES;
    [circleContentView addSubview:_socCircleView];
    [_socCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.height.offset(kScaleW(130));
        make.top.equalTo(chgTipsLab.mas_bottom).offset(15);
        make.width.equalTo(weakSelf.socCircleView.mas_height).multipliedBy(1.6);
    }];
    
    //充放电功率
    QMUILabel *chgPowerLab = [[QMUILabel alloc] init];
    [chgPowerLab setText:[NSString stringWithFormat:@"%@(kW)",@"充放电功率".rh_localized]];
    [chgPowerLab setTextAlignment:NSTextAlignmentCenter];
    [chgPowerLab setFont:kSYSTEMFONT(16)];
    [chgPowerLab setTextColor:kCOLOR_HEX_A(@"3F4443", 0.8)];
    [contentView addSubview:chgPowerLab];
    [chgPowerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(circleContentView.mas_bottom).offset(15);
    }];
    
    QMUILabel *chgPowerTipsLab = [[QMUILabel alloc] init];
    [chgPowerTipsLab setText:@"设置充放电功率，当电池进行充放电时，系统将按此功率进行。".rh_localized];
    [chgPowerTipsLab setTextAlignment:NSTextAlignmentCenter];
    [chgPowerTipsLab setFont:kSYSTEMFONT(12)];
    [chgPowerTipsLab setTextColor:kCOLOR_HEX_A(@"3F4443", 0.8)];
    [contentView addSubview:chgPowerTipsLab];
    [chgPowerTipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(chgPowerLab.mas_bottom).offset(15);
    }];
    
    QMUITextField *textField = [[QMUITextField alloc]init];
    textField.layer.cornerRadius = 8;
    textField.backgroundColor = CFTabViewBgColor;
    textField.placeholder = @"请输入功率".rh_localized;
    textField.placeholderColor = kCOLOR_HEX_A(@"3F4443",0.3);
    textField.font = kSYSTEMFONT(14);
    textField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [contentView addSubview:textField];
    self.textField = textField;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(chgPowerTipsLab.mas_bottom).offset(15);
        make.height.offset(45);
    }];
    
    QMUIButton *closeBtn = [[QMUIButton alloc]init];
    [closeBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [contentView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.right.mas_offset(-10);
        make.centerY.equalTo(weakSelf.titleLab);
    }];
    
    UIView *btnView = kView;
    btnView.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textField.mas_bottom).offset(20);
        make.left.right.bottom.mas_offset(0);
    }];
    
    QMUIButton *cancelBtn = [[QMUIButton alloc]init];
    [cancelBtn setTitle:@"取消".rh_localized forState:UIControlStateNormal];
    [cancelBtn setTitleColor:kCOLOR_HEX(@"CCCCCC") forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = kSYSTEMFONT(16);
    cancelBtn.layer.borderColor = kCOLOR_HEX(@"CCCCCC").CGColor;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.cornerRadius = 8;
    [btnView addSubview:cancelBtn];
    CGFloat bottom_margin = 0;
    if(kSafe_bottom == 0){
        bottom_margin = 10;
    }
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(25);
        make.height.offset(kScaleW(43));
        make.bottom.offset(-bottom_margin-kSafe_bottom);
    }];
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    sureBtn.titleLabel.font = kSYSTEMFONT(15);
    [sureBtn setTitleColor:FontColor(@"#ffffff") forState:UIControlStateNormal];
    sureBtn.layer.cornerRadius = 8;
    [sureBtn setBackgroundColor:CMainColorGreen];
    [contentView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cancelBtn.mas_right).offset(35);
        make.width.equalTo(cancelBtn);
        make.centerY.equalTo(cancelBtn);
        make.right.mas_offset(-25);
        make.height.offset(45);
    }];
    
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];

    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(49);
        make.top.mas_offset(0);
    }];

    
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        
        [weakSelf hidden];
    }];
    
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.curMode){
            if (weakSelf.sureBlock) {
                weakSelf.curMode.soc = [NSString stringWithFormat:@"%ld",(NSInteger)(weakSelf.socCircleView.progress*100)];
                weakSelf.curMode.power =  [[NSNumber numberWithFloat:self.socCircleView.power]twoDecimalPlacesWithoutRounding];
                weakSelf.sureBlock(weakSelf.curMode);
            }
            [weakSelf hidden];
        }
    }];

    [[RACSignal merge:@[textField.rac_textSignal,RACObserve(textField, text)]]subscribeNext:^(NSString * _Nullable x) {
        weakSelf.socCircleView.power = [x floatValue];
    }];
}




- (void)setCurModel:(RHPolicyModeModel *)curMode{
    _curMode = curMode;
   
}



- (void)hidden{
    kWeakSelf;
    [self.bgViewBottomConstraint uninstall];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);

    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self setNeedsLayout];
        [self layoutIfNeeded];
        [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    } completion:^(BOOL finished) {
      
        [self removeFromSuperview];
       
    }];
}


- (void)showAnimation{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.bgViewTopConstraint uninstall];
            [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                self.bgViewBottomConstraint = make.bottom.mas_offset(0);

            }];
            [UIView animateWithDuration:0.2 animations:^{
                [self setNeedsLayout];
                [self layoutIfNeeded];
                [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
            }];
    });

}



- (void)hiddenAnimation{
    
    
}




-(void)dealloc{
    NSLog(@"---->PolicyConfigView dealloc");
}

@end
